<?php
// +----------------------------------------------------------------------
// | 有理想的地方，地狱都是天堂。
// +----------------------------------------------------------------------
// | Copyright @ df81.com 版权所有
// +----------------------------------------------------------------------
// | 作者: 南国老符 邮箱：1062129401@qq.com
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\BaseController;
use app\admin\model\module as moduleModel;
use Overtrue\Pinyin\Pinyin;
use think\facade\Lang;
use think\facade\Db;
use think\facade\View;

class Base extends BaseController {
    public function initialize(){
        error_reporting(E_ERROR | E_PARSE );
        $this->lang = $lang = input('lang'); $userid = session('userid'); $groupid = session('groupid');
        //判断用户是否登陆
        if(!isset($userid)){
            if(request()->isAjax()){
                return json(lang('c_login_again'));
            }else{
                $this->redirect(url('/login/index'));
            }
        }
        
        //默认语言
        $defaultLangWhere[] = ['isdefault','=',1];
        $muluRs = Db::name('language')->where($defaultLangWhere)->find();
        if(isLang($lang) == false){
            $this->lang = $lang = $muluRs['mulu'];
        }

        //头部语言循环
        $headlang = Db::name('language')->where('signid',1)->order('sequence desc')->limit(100)->select()->toArray();
        $this->location = array('localhost','127.0.0.1');
        View::assign('headlang',$headlang);
        //启用的语言数量
        View::assign('languageNum',count($headlang));
        $activeLangArr = array_column($headlang, 'mulu');

        //判断是否获取语言
        if(!in_array($lang, $activeLangArr)){
            $lang = config('default_lang');
            $langUrl = request()->url(true);
            $haveStr = strpos($langUrl,'?');
            if($haveStr === false){
                $langUrl .= '?lang='.$muluRs['mulu'];
            }else{
                if(empty($lang)){
                    $langUrl .= '&lang='.$muluRs['mulu'];
                }else{
                    $langUrl = url('/index/index',['lang'=>$muluRs['mulu']]);
                }
            }
            $this->redirect($langUrl);
        }
        View::assign('lang',$lang);
        // 加载语言包
        Lang::load(app()->getAppPath().'lang'.DS.$lang.'.php');

        //头部当前语言
        $nowLangWhere[] = ['mulu','=',$lang];
        $nowLang = Db::name('language')->where($nowLangWhere)->find();
        $this->protocol = 'https://';
        View::assign('nowLang',$nowLang);

        //（左侧）启用模块循环
        $moduleWhere[] = ['lang','=',$lang]; $moduleWhere[] = ['disabled','=',1];
        View::assign('modulelist',Db::name('module')->where($moduleWhere)->order('sequence desc')->select());
        
        //导航变量
        // 首字母大写，下划线去掉，下划线后第一个字母改为大写
        $general = new \common\General();
        $tabledir = $general->capital_to_underline(input('tabledir'));
        $moduletable = $general->capital_to_underline(input('moduletable'));
        $controller = $general->capital_to_underline(request()->controller());
        $action = $general->capital_to_underline(request()->action());
        View::assign('controller',$controller);
        View::assign('action',$action);
        View::assign('tabledir',$tabledir);
        View::assign('moduletable',$moduletable);
        View::assign('system_type',config('app.other.system_type'));

        $navmoduleWhere[] = ['lang','=',$lang];
        if($tabledir != 'shop_spec'){
            $navmoduleWhere[] = ['tabledir','=',$tabledir];
        }else{
            $navmoduleWhere[] = ['tabledir','=',$moduletable];
        }
        $this->navModuleRs = $navModuleRs = Db::name('module')->where($navmoduleWhere)->find();
        $rootDomain = request()->rootDomain();
        if(is_numeric(substr($rootDomain,-1))) $rootDomain = request()->server('SERVER_NAME');
        $this->rootDomain = $rootDomain;
        View::assign('domain',request()->domain());
        View::assign('navModuleRs',$navModuleRs);

        //后台入口文件名是否已经修改
        $admintips = is_file(request()->server('DOCUMENT_ROOT').'/admin.php') ? 'yes' : '';
        View::assign('admintips',$admintips);

        //头部状态
        $home_current = $data_current = $global_current = $system_current = '';
        $dataTableArr = array('shop_order','shop_aftersale','shop_tax','user','feedback','shop_coupon','shop_mycoupon','comment','capitalflow','shop_address','log','license');
        $globalTableArr = array('site','field','chat','ad','keyword','navigation','area','module','filemanage','config','databackup','language','templatemanage','auth_group','auth_rule','sitemap');
        $systemTableArr = array('shop_set','social','email','sms','payment','send_template','logistics','mpweixin','system');
        if(in_array($controller,$dataTableArr) or ($controller == 'attribute' and in_array($tabledir,$dataTableArr))){
            $data_current = 'active';
        }
        if(in_array($controller,$globalTableArr) or ($controller == 'attribute' and in_array($tabledir,$globalTableArr))){
            $global_current = 'active';
        }
        if(in_array($controller,$systemTableArr) or ($controller == 'attribute' and in_array($tabledir,$systemTableArr))){
            $system_current = 'active';
        }
        if($data_current != 'active' and $global_current != 'active' and $system_current != 'active'){
            $home_current = 'active';
        }
        View::assign('home_current',$home_current);
        View::assign('data_current',$data_current);
        View::assign('global_current',$global_current);
        View::assign('system_current',$system_current);

        //权限控制
        $auth = new \common\Auth();
        $rule  = $controller.'/'.$action;
        $check_user = $auth->check($rule,$userid);

        switch (session('is_admin')) {
            // 超级管理员组允许访问任何页面
            case 9:
                return true;
                break;
            // 禁止访问会员组
            case 3:
                $this->error(lang('g_no_access'),url('/login/index'));
                break;
        }
        
        if(!$check_user){
            $this->error(lang('c_no_auth'),url('/login/index'));
        }
    }

    //汉字转拼音
    public function pinyin(){
        $pinyin = new Pinyin();
        if(input('lang') <> 'cn'){
            $urlroute = $pinyin->permalink(input('title'),'-');
            if(input('from') == 'module') $urlroute = str_replace('-','',$urlroute);
        }else{
            switch (config('global.url.pinyin_type')) {
                // 首字母
                case 'abbr':
                    $urlroute = $pinyin->abbr(input('title'),'');
                    break;
                // 全拼
                default:
                    $urlroute = $pinyin->permalink(input('title'),'');
                    break;
            }
        }
        $urlroute = strtolower(msubstr($urlroute,0,config('global.url.length'),''));
        echo $urlroute;
    }
}